package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.service.ISetmealDishService;
import com.sky.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
@Autowired
private ISetmealDishService iSetmealDishService;

    @Override
    @Transactional
    public void addSetmealAndDish(SetmealDTO setmealDTO) {
        //setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setId(null);
        this.save(setmeal);
        //dish
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(s->{
            s.setSetmealId(setmeal.getId());
            s.setId(null);
        });
        iSetmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void updateSetmealAndDish(SetmealDTO setmealDTO) {
        //setmeal修改
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        this.updateById(setmeal);
        //setmealDish修改
            //删除
        LambdaQueryWrapper<SetmealDish> lqw =new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId,setmeal.getId());
        iSetmealDishService.remove(lqw);
        //添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(s->{s.setId(null);s.setSetmealId(setmeal.getId());});
        iSetmealDishService.saveBatch(setmealDishes);
    }


}
